package gatoropoly.ui;

import static gatoropoly.Log.warn;

import gatoropoly.ai.SimpleAI;
import gatoropoly.game.AbstractGatoropolyListener;
import gatoropoly.game.Gatoropoly;
import gatoropoly.game.GatoropolyClient;
import gatoropoly.game.GatoropolyListenerDispatcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;


public class Main extends AbstractGatoropolyListener {

	private final GatoropolyListenerDispatcher dispatcher = new GatoropolyListenerDispatcher();

	private final String name;

	private final Gatoropoly client;

	private String cookie;

	private final GUI gui;

	private final TextUI tui;

	private final PrintWriter out;

	private SimpleAI ai;

	public Main(String hostname, int port, String name) throws IOException,
			ParserConfigurationException {
		String logfile = hostname + "_" + name + ".log";
		this.out = new PrintWriter(new FileWriter(logfile));
		this.name = name;
		this.client = new GatoropolyClient(hostname, port, dispatcher);

		dispatcher.addListener(this);

		gui = new GUI(client);
		gui.setSize(700, 400);
		gui.setVisible(true);

		tui = new TextUI(client, dispatcher, new InputStreamReader(System.in),
				new OutputStreamWriter(System.out));
		tui.start();

		readCookie();

		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
					deleteCookie();
			}
		});
		client.connect();
	}

	public void serverVersion(String version) {
		if (cookie != null)
			client.reconnect(cookie);
		else
			client.setName(name);
	}

	public void clientInfo(int playerId, String cookie) {
		if (this.cookie != null && !this.cookie.equals(cookie))
			client.setName(name);
		this.cookie = cookie;
		writeCookie();
		gui.loggedIn();
		gui.setState(new ConnectedState(client));
	}

	public void xml(String xml) {
		out.println(xml);
		out.flush();
	}

	void writeCookie() {
		PrintWriter out = null;
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					name + ".cookie")));
			out.println(cookie);
		} catch (IOException e) {
			warn("Could not write cookie");
		}
		if (out != null)
			out.close();
	}

	void deleteCookie() {
		new File(name + ".cookie").delete();
	}

	public void startAi() {
		if (this.ai != null)
			dispatcher.removeListener(ai);
		ai = new SimpleAI(client, dispatcher);
	}

	void readCookie() {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(
					name + ".cookie")));
			this.cookie = in.readLine();
		} catch (IOException e) {
			warn("Could not read cookie");
		}
		if (in != null)
			try {
				in.close();
			} catch (IOException e) {
			}
	}

	public static void main(String[] args) throws IOException,
			ParserConfigurationException {
		boolean startAi = false;
		String hostname = "localhost";
		int port = 1234;
		String name = "JGatoropoly";

		int v = 0;
		if (args.length > 0 && args[v++].equals("-ai"))
			startAi = true;
		if (args.length > 1)
			hostname = args[v++];
		if (args.length == 3)
			port = Integer.parseInt(args[v++]);
		if (args.length > v)
			name = args[v++];

		if (startAi)
			name = name + "Bot";
		Main main = new Main(hostname, port, name);
		if (startAi)
			main.startAi();

	}

}
